package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.util.AuthContextHolder;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-09-06 19:46
 * @Description:
 */
@RestController
@RequestMapping("api/user")
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        Map<String,Object> info = userInfoService.login(loginVo);
        return R.ok().data(info);
    }


    //用户认证
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //从请求中获取用户id
        Long userId = AuthContextHolder.getUserId(request);

        userInfoService.userAuth(userId,userAuthVo);
        return R.ok();
    }

    //获取用户信息
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);

        UserInfo userInfo = userInfoService.getById(userId);

        Integer status_auth = userInfo.getStatus();

        // 设置 认证状态 0：未认证 1：认证中 2：认证成功 -1：认证失败
        String authStatusString = "";
        AuthStatusEnum[] values = AuthStatusEnum.values();
        for (AuthStatusEnum value : values) {
            Integer status = value.getStatus();
            if(status.intValue() == status_auth.intValue()){
                authStatusString = value.getName();
            }
        }

        userInfo.getParam().put("authStatusString",authStatusString);

        return R.ok().data("userInfo",userInfo);
    }

}
